package ${generator.packageName}.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import ${generator.commonPackage}.base.utils.DxracerParamPage;
import ${generator.commonPackage}.base.utils.DxracerResponse;
import ${generator.commonPackage}.base.utils.DxracerResponsePage;
import ${generator.basePackageName}.entity.${generator.className}DO;
import ${generator.basePackageName}.mapper.${generator.className}Mapper;
import ${generator.packageName}.entity${(generator.childPackageName)!""}.dto.${generator.className}InsertDTO;
import ${generator.packageName}.entity${(generator.childPackageName)!""}.dto.${generator.className}UpdateDTO;
import ${generator.packageName}.entity${(generator.childPackageName)!""}.dto.${generator.className}QueryDTO;
import ${generator.packageName}.entity${(generator.childPackageName)!""}.vo.${generator.className}GetVO;
import ${generator.packageName}.entity${(generator.childPackageName)!""}.relation.${generator.className}RelationDO;
import ${generator.packageName}.mapper.${generator.className}RelationMapper;
import ${generator.packageName}.service.${generator.className}Service;

import lombok.RequiredArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

/**
 * ${generator.tableComment} 业务接口实现类
 *
 * @author ${generator.author}
 * @version 0.0.1
 * @date ${.now?string["yyyy-MM-dd HH:mm:ss"]}
 * @since ${.now?string["yyyy-MM-dd HH:mm:ss"]}
 */
@Service("${generator.className?uncap_first}Service")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ${generator.className}ServiceImpl extends ServiceImpl<${generator.className}Mapper, ${generator.className}DO> implements ${generator.className}Service {

<#if relations?? && (relations?size > 0)>
    private final ${generator.className}RelationMapper ${generator.className?uncap_first}RelationMapper;

</#if>
<#if generator.functions?seq_contains("insert")>
    @Override
    public DxracerResponse<String> insert(@RequestBody @Valid ${generator.className}InsertDTO ${generator.className?uncap_first}InsertDTO) {
        ${generator.className}DO ${generator.className?uncap_first}InsertDO = JSONUtil.toBean(JSONUtil.parseObj(${generator.className?uncap_first}InsertDTO), ${generator.className}DO.class);
        return this.save(${generator.className?uncap_first}InsertDO) ? DxracerResponse.success("新增成功") : DxracerResponse.error(500, "新增失败");
    }

</#if>
<#if generator.functions?seq_contains("update")>
    @Override
    public DxracerResponse<String> modify(@RequestBody @Valid ${generator.className}UpdateDTO ${generator.className?uncap_first}UpdateDTO) {
        ${generator.className}DO ${generator.className?uncap_first}UpdateDO = JSONUtil.toBean(JSONUtil.parseObj(${generator.className?uncap_first}UpdateDTO), ${generator.className}DO.class);
        return this.updateById(${generator.className?uncap_first}UpdateDO) ? DxracerResponse.success("修改成功") : DxracerResponse.error(500, "修改失败");
    }

</#if>
<#if generator.functions?seq_contains("delete")>
    @Override
    public DxracerResponse<String> deleteById(Integer id) {
        return this.removeById(id) ? DxracerResponse.success("修改成功") : DxracerResponse.error(500, "修改失败");
    }

</#if>
<#if generator.functions?seq_contains("get")>
    @Override
    public DxracerResponse<${generator.className}GetVO> selectOne(${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO) {
        QueryWrapper<${generator.className}DO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(${generator.className?uncap_first}QueryDTO.getId() != null, ${generator.className}DO::getId, ${generator.className?uncap_first}QueryDTO.getId());
        return DxracerResponse.data(JSONUtil.toBean(JSONUtil.toJsonStr(this.getOne(wrapper)), ${generator.className}GetVO.class));
    }

    <#if relations?? && (relations?size > 0)>
    @Override
    public DxracerResponse<${generator.className}GetVO> selectRelation(${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO) {
        QueryWrapper<${generator.className}DO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(${generator.className?uncap_first}QueryDTO.getId() != null, ${generator.className}DO::getId, ${generator.className?uncap_first}QueryDTO.getId());
        return DxracerResponse.data(JSONUtil.toBean(JSONUtil.toJsonStr(${generator.className?uncap_first}RelationMapper.getRelation(wrapper, true)), ${generator.className}GetVO.class));
    }

    </#if>
</#if>
<#if generator.functions?seq_contains("page")>
    @Override
    public DxracerResponsePage<${generator.className}GetVO> getPage(${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO, DxracerParamPage dxracerParamPage) {
        Page<${generator.className}DO> page = new Page<>(dxracerParamPage.getPage(), dxracerParamPage.getRowsPerPage(), true);
        QueryWrapper<${generator.className}DO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(${generator.className?uncap_first}QueryDTO.getId() != null, ${generator.className}DO::getId, ${generator.className?uncap_first}QueryDTO.getId());
        if (StrUtil.isNotBlank(dxracerParamPage.getSortBy())) {
            if (dxracerParamPage.isDescending()) {
                wrapper.orderByDesc(dxracerParamPage.getSortBy());
            } else if (!dxracerParamPage.isDescending()) {
                wrapper.orderByAsc(dxracerParamPage.getSortBy());
            }
        }

        IPage result = this.page(page, wrapper);

        return DxracerResponsePage
                .<${generator.className}GetVO>builder()
                .total(result.getTotal())
                .data(JSONUtil.toList(JSONUtil.parseArray(result.getRecords()), ${generator.className}GetVO.class))
                .build();
    }

    <#if relations?? && (relations?size > 0)>
    @Override
    public DxracerResponsePage<${generator.className}GetVO> getRelationPage(${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO, DxracerParamPage dxracerParamPage) {
        Page<${generator.className}RelationDO> page = new Page<>(dxracerParamPage.getPage(), dxracerParamPage.getRowsPerPage(), true);
        QueryWrapper<${generator.className}RelationDO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(${generator.className?uncap_first}QueryDTO.getId() != null, ${generator.className}RelationDO::getId, ${generator.className?uncap_first}QueryDTO.getId());
        if (StrUtil.isNotBlank(dxracerParamPage.getSortBy())) {
            if (dxracerParamPage.isDescending()) {
                wrapper.orderByDesc(dxracerParamPage.getSortBy());
            } else if (!dxracerParamPage.isDescending()) {
                wrapper.orderByAsc(dxracerParamPage.getSortBy());
            }
        }

        IPage result = ${generator.className?uncap_first}RelationMapper.getRelation(page, wrapper);

        return DxracerResponsePage
                .<${generator.className}GetVO>builder()
                .total(result.getTotal())
                .data(JSONUtil.toList(JSONUtil.parseArray(result.getRecords()), ${generator.className}GetVO.class))
                .build();
    }

    </#if>
</#if>
<#if generator.functions?seq_contains("list")>
    @Override
    public DxracerResponse<List<${generator.className}GetVO>> getList(${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO) {
        QueryWrapper<${generator.className}DO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(${generator.className?uncap_first}QueryDTO.getId() != null, ${generator.className}DO::getId, ${generator.className?uncap_first}QueryDTO.getId());

        List<${generator.className}DO> result = this.list(wrapper);

        return DxracerResponse.data(JSONUtil.toList(JSONUtil.parseArray(result), ${generator.className}GetVO.class));
    }

    <#if relations?? && (relations?size > 0)>
    @Override
    public DxracerResponse<List<${generator.className}GetVO>> getRelationList(${generator.className}QueryDTO ${generator.className?uncap_first}QueryDTO) {
        QueryWrapper<${generator.className}DO> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(${generator.className?uncap_first}QueryDTO.getId() != null, ${generator.className}DO::getId, ${generator.className?uncap_first}QueryDTO.getId());

        List<${generator.className}RelationDO> result = ${generator.className?uncap_first}RelationMapper.getRelation(wrapper);

        return DxracerResponse.data(JSONUtil.toList(JSONUtil.parseArray(result), ${generator.className}GetVO.class));
    }

    </#if>
</#if>
<#if generator.functions?seq_contains("batchInsert")>

</#if>
<#if generator.functions?seq_contains("batchUpdate")>

</#if>
<#if generator.functions?seq_contains("batchDelete")>

</#if>
<#if generator.functions?seq_contains("import")>

</#if>
<#if generator.functions?seq_contains("export")>

</#if>
}
